package cn.iocoder.yudao.module.enterprise.service.praxisProjectSchedule;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProject.vo.EnterpriseProjectScheduleVO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.praxisProjectSchedule.EnterpriseProjectScheduleDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.praxisProjectSchedule.EnterpriseProjectScheduleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目赛程安排 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class EnterpriseProjectScheduleServiceImpl implements EnterpriseProjectScheduleService {

    @Resource
    private EnterpriseProjectScheduleMapper enterpriseProjectScheduleMapper;

    @Override
    public void save(EnterpriseProjectScheduleDO d) {
        enterpriseProjectScheduleMapper.insert(d);
    }

    @Override
    public void batchSaveProjectSchedule(List<EnterpriseProjectScheduleDO> list) {
        enterpriseProjectScheduleMapper.insertBatch(list);
    }

    @Override
    public void deleteProjectScheduleByProjectId(Long projectId) {
        enterpriseProjectScheduleMapper.delete(new QueryWrapper<EnterpriseProjectScheduleDO>().eq("project_id",projectId));
    }

    @Override
    public List<EnterpriseProjectScheduleDO> getPraxisProjectScheduleList(EnterpriseProjectScheduleVO exportReqVO) {
        return enterpriseProjectScheduleMapper.selectList(exportReqVO);
    }

    @Override
    public List<EnterpriseProjectScheduleVO> getScheduleListByProjectId(Long projectId) {
        return enterpriseProjectScheduleMapper.getScheduleListByProjectId(projectId);
    }

    @Override
    @TenantIgnore
    public EnterpriseProjectScheduleVO getNextScheduleByProjectId(Long projectId, Integer scheduleType) {
        EnterpriseProjectScheduleDO scheduleDO = enterpriseProjectScheduleMapper.selectOne(new QueryWrapper<EnterpriseProjectScheduleDO>()
                .eq("project_id", projectId).ge("schedule_type", scheduleType).orderByAsc("schedule_type").last(" limit 1"));
        EnterpriseProjectScheduleVO scheduleVO=null;
        if(null!=scheduleDO){
            scheduleVO=new EnterpriseProjectScheduleVO();
            BeanUtils.copyProperties(scheduleDO,scheduleVO);
        }
        return  scheduleVO;
    }
}
